2 * Copyright (c) 2017 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
26 #import "SFTransactionMetric.h"
27 #import <os/log_private.h>
29 @interface SFTransactionMetric ()
30 @property (nonatomic, copy) NSString *uuid;
31 @property (nonatomic, copy) NSString *category;
32 @property (nonatomic, strong) os_log_t logObject;
34 -(os_log_t) sftmCreateLogCategory:(NSString*) category;
35 -(os_log_t) sftmObjectForCategory:(NSString*) category;
38 static NSMutableDictionary *logObjects;
39 static const NSString* signInLogSpace = @"com.apple.security.wiiss";
41 @implementation SFTransactionMetric
43 + (BOOL)supportsSecureCoding {
47 -(os_log_t) sftmObjectForCategory:(NSString*) category
49 return logObjects[category];
52 -(os_log_t) sftmCreateLogCategory:(NSString*) category
54 return os_log_create([signInLogSpace UTF8String], [category UTF8String]);
57 - (instancetype)initWithUUID:(NSString *)uuid category:(NSString *)category
64 static dispatch_once_t onceToken;
65 dispatch_once(&onceToken, ^{
66 logObjects = [NSMutableDictionary dictionary];
68 @synchronized(logObjects){
70 _logObject = [self sftmObjectForCategory:category];
73 _logObject = [self sftmCreateLogCategory:category];
74 [logObjects setObject:_logObject forKey:category];
82 - (void)encodeWithCoder:(NSCoder *)coder {
83 [coder encodeObject:_uuid forKey:@"UUID"];
84 [coder encodeObject:_category forKey:@"category"];
87 - (nullable instancetype)initWithCoder:(NSCoder *)decoder
91 _uuid = [decoder decodeObjectOfClass:[NSString class] forKey:@"UUID"];
92 _category = [decoder decodeObjectOfClass:[NSString class] forKey:@"category"];
97 - (void)logEvent:(NSString*)eventName eventAttributes:(NSDictionary<NSString*, id>*)attributes
99 [attributes enumerateKeysAndObjectsUsingBlock:^(NSString* key, id obj, BOOL * stop) {
100 os_log(self.logObject, "event: %@, %@ : %@", eventName, key, obj);
104 - (void)timeEvent:(NSString*)eventName blockToTime:(void(^)(void))blockToTime
106 NSDate *firstTime = [NSDate date];
110 NSDate *SecondTime = [NSDate date];
112 os_log(self.logObject, "event: %@, Time elapsed: %@", eventName, [[NSString alloc] initWithFormat:@"%f", [SecondTime timeIntervalSinceDate:firstTime]]);
115 - (void)logError:(NSError*)error
117 os_log_error(self.logObject, "%@", error);
120 - (void)signInCompleted
123 os_log(self.logObject, "sign in complete for %@", self.uuid);